package com.trafficsys.modules.police.service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import com.trafficsys.modules.entity.DriveCard;
import com.trafficsys.modules.entity.Driver;
import com.trafficsys.modules.entity.Police;
import com.trafficsys.modules.entity.TravelCard;
import com.trafficsys.modules.police.dao.DriverAndIdCardDao;
import com.trafficsys.modules.police.pojo.DriverAndIdCard;
import com.trafficsys.modules.police.pojo.PoliceAndIdCard;
import com.trafficsys.modules.police.service.DriverAndIdCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class DriverAndIdCardServiceImpl implements DriverAndIdCardService {
    @Autowired
    private DriverAndIdCardDao driverAndIdCardDao;
    @Override
    public HashMap<String, Object> insertDriver(Driver driver) {
        HashMap<String, Object> map = new HashMap<>();
        int num = driverAndIdCardDao.insert(driver);
        if (num!=0){
            map.put("info","添加成功");
        }else {
            map.put("info","添加失败");
        }
        return map;
    }

    @Override
    public IPage<DriverAndIdCard> selectDriverByCardId(Page<DriverAndIdCard> page) {
        return driverAndIdCardDao.selectDriverByCardId(page);
    }

    @Override
    public HashMap<String, Object> updateDriverAndIdCard(DriverAndIdCard driverAndIdCard) {
        HashMap<String, Object> map = new HashMap<>();
        try{
            int num = driverAndIdCardDao.updateDriver(driverAndIdCard);
            int num1 = driverAndIdCardDao.updateIdCard(driverAndIdCard);
            if (num!=0||num1!=0){
                map.put("info","修改成功");
            }else {
                map.put("info","修改失败");
            }
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("info","手机号已经注册！");
            return map;
        }

    }

    @Override
    public HashMap<String, Object> deleteDriverAndIdCard(String cardId) {
        HashMap<String, Object> map = new HashMap<>();
        int num = driverAndIdCardDao.deleteDriver(cardId);
        if (num!=0){
            map.put("info","删除成功");
        }else {
            map.put("info","删除失败");
        }
        return map;
    }




    @Override
    public IPage<PoliceAndIdCard> getPolice(Page<PoliceAndIdCard> page) {
        return driverAndIdCardDao.getPolice(page);
    }

    @Override
    public HashMap<String, Object> updatePoliceAndIdCard(PoliceAndIdCard policeAndIdCard) {
        HashMap<String, Object> map = new HashMap<>();
        try{
            int num = driverAndIdCardDao.updatePolice(policeAndIdCard);
            int num1 = driverAndIdCardDao.updatePoliceIdCard(policeAndIdCard);
            if (num!=0||num1!=0){
                map.put("info","修改成功");
            }else {
                map.put("info","修改失败");
            }
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("info","手机号已经注册！");
            return map;
        }
    }

    @Override
    public HashMap<String, Object> deletePoliceAndIdCard(String cardId) {
        HashMap<String, Object> map = new HashMap<>();
        int num = driverAndIdCardDao.deletePolice(cardId);
        if (num!=0){
            map.put("info","删除成功");
        }else {
            map.put("info","删除失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> insertTravelCard(TravelCard travelCard) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            int num = driverAndIdCardDao.insertTravelCard(travelCard);
            if (num!=0){
                map.put("info","添加成功");
            }else {
                map.put("info","添加失败");
            }
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("info","车牌号已经存在");
            return map;
        }

    }

    @Override
    public HashMap<String, Object> updateTravelCard(TravelCard travelCard) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            int num = driverAndIdCardDao.updateTravelCard(travelCard);
            if (num!=0){
                map.put("info","修改成功");
            }else {
                map.put("info","修改失败");
            }
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("info","该车牌号已经存在");
            return map;
        }

    }

    @Override
    public HashMap<String, Object> deleteTravelCard(String carNo) {
        HashMap<String, Object> map = new HashMap<>();
        int num = driverAndIdCardDao.deleteTravelCard(carNo);
        if (num!=0){
            map.put("info","删除成功");
        }else {
            map.put("info","删除失败");
        }
        return map;
    }

    @Override
    public IPage<DriveCard> getDriveCard(Page<DriveCard> page) {
        return driverAndIdCardDao.getDriveCard(page);
    }

    @Override
    public HashMap<String, Object> insertDriverCard(DriveCard driveCard) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            int num = driverAndIdCardDao.insertDriverCard(driveCard);
            if (num!=0){
                map.put("info","添加成功");
            }else {
                map.put("info","添加失败");
            }
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("info","驾驶证号已存在");
            return map;
        }

    }

    @Override
    public HashMap<String, Object> updateDriverCard(DriveCard driveCard) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            int num = driverAndIdCardDao.updateDriverCard(driveCard);
            if (num!=0){
                map.put("info","修改成功");
            }else {
                map.put("info","修改失败");
            }
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("info","驾驶证号已存在");
            return map;
        }

    }

    @Override
    public HashMap<String, Object> deleteDriverCard(String cardId) {
        HashMap<String, Object> map = new HashMap<>();
        int num = driverAndIdCardDao.deleteDriverCard(cardId);
        if (num!=0){
            map.put("info","删除成功");
        }else {
            map.put("info","删除失败");
        }
        return map;
    }
}
